﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCE.UI.PopWindows
{
    public partial class NoteFeedback : EFViewCmes
    {
        public NoteFeedback(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 实例化数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }

        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick(sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 控制显示异常详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void more_ValueChanged(object sender, Platform.Core.UI.Controls.ValueChangedEventArgs e)
        {
            if (!e.OldValue.Equals(""))
            {
                if (e.NewValue.ToString().Equals("1"))
                {
                    this.InsertGridMore.Visibility = Visibility.Visible;
                    this.fdDegree.eRegex = "^.+$";
                    this.fdDegree.eValidateErrorPrompt = "输入不能为空";
                    this.fdExceptionDescription.eRegex = ".{1,125}$";
                    this.fdExceptionDescription.eValidateErrorPrompt = "输入1~125个字符";
                }
                else
                {
                    this.InsertGridMore.Visibility = Visibility.Collapsed;
                    this.fdDegree.eRegex = "^.*$";
                    this.fdDegree.eValidateErrorPrompt = "";
                    this.fdExceptionDescription.eRegex = "^.{0,125}$";
                    this.fdExceptionDescription.eValidateErrorPrompt = "输入0~125个字符";
                }
            }
        }

        /// <summary>
        /// 判定EFDataField的值是否为空
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        private bool NotNullOrEmpty(EFDataField field)
        {
            bool flag = false;
            if ((field.Value != null) && (!field.Value.ToString().Trim().Equals("")))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 给工单状态赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdCondition_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (NotNullOrEmpty(this.fdCondition))
            {
                if ("完成".Equals(this.fdCondition.Value.ToString()))
                {
                    this.fdStatus.Value = "完成";
                    this.fdTrueEndTime.Visibility = Visibility.Visible;
                    this.equipmentStatus.Visibility = Visibility.Visible;
                    this.fdTrueEndTime.eLabel = "*完成时间";
                    this.fdTrueEndTime.eRegex = @"^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)\s+([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$";
                    this.fdTrueEndTime.eValidateErrorPrompt = "输入格式为yyyy-MM-dd HH:mm:ss的日期时间";
                    if (!(fdType.Value.ToString().Equals("大修") || fdType.Value.ToString().Equals("维修")))
                    {
                        this.equipmentStatus.Visibility = Visibility.Collapsed;
                    }
                    this.fdDescription.eLabel = "工作描述";
                    this.fdDescription.eRegex = @"^.{0,125}$";
                    this.fdDescription.eValidateErrorPrompt = "输入1~125个字符";
                }
                else if ("暂停".Equals(this.fdCondition.Value.ToString()))
                {
                    this.fdStatus.Value = "暂停";
                    this.fdTrueEndTime.Visibility = Visibility.Collapsed;
                    this.equipmentStatus.Visibility = Visibility.Collapsed;
                    this.fdTrueEndTime.eLabel = "完成时间";
                    this.fdTrueEndTime.eRegex = @"^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)\s+([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$";
                    this.fdTrueEndTime.eValidateErrorPrompt = "输入格式为yyyy-MM-dd HH:mm:ss的日期时间";
                    this.fdTrueEndTime.Value = "";
                    this.fdDescription.eLabel = "*工作描述";
                    this.fdDescription.eRegex=@"^.{1,125}$";
                    this.fdDescription.eValidateErrorPrompt = "输入1~125个字符";
                }
            }
        }

        /// <summary>
        /// 执行人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdExecutionPersonId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null && !"".Equals(picker.ItemObject))
            {
                this.fdExecutionPerson.Value = picker.ItemObject.GetType().GetProperty("empName").GetValue(picker.ItemObject, null).ToString();
            }
        }

    }
}
